package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import java.util.UUID;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseUsers;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Shiros;
import top.jnaw.jee.utils.Strings;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Users extends BaseUsers<Users> {

  public static final Users dao = new Users().dao();

  public static ModelResult add(String mobile, String name) {
    ModelResult result = new ModelResult(Result.kModelSaveError);
    Users user = getByMobile(mobile);
    if (null == user) {
      final String username = UUID.randomUUID().toString().replace("-", "");
      user = new Users();
      user.setMobile(mobile);
      user.setName(name);
      user.setUsername(username);
      user.setPasswordSalt(Shiros.genSalt(username));
      boolean r = user.save();
      if (r) {
        r = Shiros.setPwd(username, user.getMobile());
      }
      if (r) {
        result.setResult(Result.kSuccess);
        result.setObject(user);
      }
    }
    return result;
  }

  public static ModelResult update(int id, String mobile, String name) {
    ModelResult result = new ModelResult(Result.kModelUpdateError);
    Users user = dao.findById(id);
    if (null != user) {
      user.setMobile(mobile);
      user.setName(name);
      boolean r = user.update();
      if (r) {
        result.setResult(Result.kSuccess);
        result.setObject(user);
      }
    }
    return result;
  }

  public static ModelResult updatePwd(String mobile, String password) {
    ModelResult result = new ModelResult(Result.kNull);
    Users users = Users.getByMobile(mobile);
    if (null != users) {
      if (Shiros.setPwd(users.getUsername(), password)) {
        users.setNeedResetPassword(false);
        result.setResult(
            users.update() ? Result.kSuccess : Result.kModelUpdateError);
      } else {
        result.setResult(Result.kModelUpdateError);
      }
    }
    return result;
  }

  public static Users getByUsername(String username) {
    Users users = null;
    if (StrKit.notBlank(username)) {
      users = Users.dao
          .findFirst("SELECT * FROM users WHERE username = ?", username);
    }
    return users;
  }

  public static Users getByMobile(String mobile) {
    Users users = null;
    if (StrKit.notBlank(mobile)) {
      users = Users.dao
          .findFirst("SELECT * FROM users" + " WHERE mobile = ?", mobile);
    }
    return users;
  }

  public static List<Users> list(String uids) {

    String sql = Db.getSql("users-listByUids")
        .replace("id = ?", "id IN " + uids);
    return dao.find(sql);
  }

  public static boolean del(int id) {
    return dao.deleteById(id);
  }

  public static JSONObject list(int page, int size, String keyWord) {
    String select = Db.getSql("user-list-sec");
    String except = Db.getSql("user-list-exp");

    if (StrKit.notBlank(keyWord)) {
      except +=
          "\n" + "AND (u.name like concat('%','" + keyWord
              + "','%') or  u.mobile like concat('%','" + keyWord + "','%'))";
    }
    Page<Record> pages = Db.paginate(page, size, select, except);
    return Models.compressPage(pages);
  }

  public static Users query(int id) {
    Users user = null;
    if (id > 0) {
      String sql = Db.getSql("users-query");
      user = dao.findById(id);
    }
    return user;
  }
}